//
//  FTVisitorView.swift
//  Weibo
//
//  Created by 方焘 on 16/6/11.
//  Copyright © 2016年 taofang. All rights reserved.
//

import UIKit
import SnapKit

/*
 // MARK: - 外部访问属性 & 重要变化属性
 
 // MARK: - 外部访问方法
 
 // MARK: - 监听方法
 
 // MARK: - 内部属性 & 懒加载控件
 
 // MARK: - 设置界面
 */

class FTVisitorView: UIView {
 // MARK: - 外部访问属性 & 重要变化属性
    var loginButtonClosure: (()->())?
    var rigiestButtonClosure: (()->())?
    
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
      //MARK: - 外部访问方法
    func setVisitorInfo(imgName: String, title: String) {
        if imgName.containsString("house") {
            circleImage.hidden = false
            setCircleAnimation()
        } else {
            circleImage.layer.removeAllAnimations()
            circleImage.hidden = true
        }
        iconImage.image = UIImage(named: imgName)
        messageLabel.text = title
    }
    //MARK: - 监听事件
   @objc private func didClickRigist() {
        self.rigiestButtonClosure?()
    }
    
   @objc private func didClickLogin() {
        self.loginButtonClosure?()
    }
      //MARK: - 布局UI
    func setupUI() {
        backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        self.addSubview(circleImage)
        self.addSubview(maskIcon)
        self.addSubview(iconImage)
        self.addSubview(messageLabel)
        self.addSubview(rigistButton)
        self.addSubview(loginButton)
        
        //添加约束
        maskIcon.snp_makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        circleImage.snp_makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        iconImage.snp_makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(maskIcon.snp_bottom).offset(10)
            make.height.equalTo(36)
            make.width.equalTo(224)
        }
        
        rigistButton.snp_makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.right.equalTo(self.snp_centerX).offset(-15)
            make.height.equalTo(40)
            make.width.equalTo(100)
        }
        loginButton.snp_makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(rigistButton)
            make.left.equalTo(self.snp_centerX).offset(15)
        }
        
    }
    
    func setCircleAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.duration = 10
        animation.repeatCount = MAXFLOAT
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false
        circleImage.layer.addAnimation(animation, forKey: "circle")
        
    }
    
  // MARK: - 内部属性 & 懒加载控件
    private lazy var maskIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var circleImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var iconImage:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return imageView
    }()
    
    private lazy var messageLabel:UILabel = {
        let label = UILabel(textColour: UIColor.grayColor(), textFont: UIFont.systemFontOfSize(14), text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
        label.numberOfLines = 0
        label.textAlignment = .Center
        return label
    }()
    
    private lazy var rigistButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        button.addTarget(self, action: #selector(FTVisitorView.didClickRigist), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        button.addTarget(self, action: #selector(FTVisitorView.didClickLogin), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()

}
